<?php
require_once ("../utils/ConnectionFactory.php");
require_once ("../persistance/data/CommandeFactureDO.php");

/**
 *
 * CRUD pour commandeFactureDO
 * @author Quentin Laujac, Charles Hurtevent
 *
 */
class CommandeFactureDAO{

	/**
	 *	retourne toute les CommandeFactureDO correspondant au numero de responsable
	 * @param entier correspondant au numero responsable
	 */
	public function getListCommandeFactureDOByNumResponsable($numResponsable){
		$connection = ConnectionFactory::getConnection ();

		$reponse = $connection->query ( "SELECT * FROM cde_fact WHERE num_resp = ".$numResponsable." ORDER BY terminee, date_cde, payee asc" );

		if ($reponse == null || $reponse->rowCount () == 0) {
			return null;
		}

		$listCommandeFactureDO = new ArrayObject ();
		while ( $donnee = $reponse->fetch () ) {
			$commandeFactureDO = $this->setCommandeFactureDO($donnee);
			$listCommandeFactureDO->append ( $commandeFactureDO );
		}
		return $listCommandeFactureDO;
	}
	
	/**
	 * 
	 * Retourne la liste des commande factureDO non facture
	 * @param entier $numResponsable
	 */
	public function getListCommandeFactureDONonFacture($numResponsable){
		$connection = ConnectionFactory::getConnection ();

		$reponse = $connection->query ( "SELECT * FROM cde_fact WHERE terminee = true AND payee = false ORDER BY date_cde asc" );

		if ($reponse == null || $reponse->rowCount () == 0) {
			return null;
		}

		$listCommandeFactureDO = new ArrayObject ();
		while ( $donnee = $reponse->fetch () ) {
			$commandeFactureDO = $this->setCommandeFactureDO($donnee);
			$listCommandeFactureDO->append ( $commandeFactureDO );
		}
		return $listCommandeFactureDO;
	}

	/**
	 * 
	 * Retourne la liste des commande factureDO facture
	 * @param entier $numResponsable
	 */
	public function getListCommandeFactureDOFacture($numResponsable){
		$connection = ConnectionFactory::getConnection ();

		$reponse = $connection->query ( "SELECT * FROM cde_fact WHERE terminee = true AND payee = true ORDER BY date_cde, colis_fait,  num_cde asc" );

		if ($reponse == null || $reponse->rowCount () == 0) {
			return null;
		}

		$listCommandeFactureDO = new ArrayObject ();
		while ( $donnee = $reponse->fetch () ) {
			$commandeFactureDO = $this->setCommandeFactureDO($donnee);
			$listCommandeFactureDO->append ( $commandeFactureDO );
		}
		return $listCommandeFactureDO;
	}


	/**
	 * Recupère le dernier numero de commande
	 */
	public function getDernierNumCommande(){
		$connection = ConnectionFactory::getConnection ();

		$reponse = $connection->query ( "SELECT * FROM cde_fact ORDER BY num_cde desc" );

		if ($reponse == null || $reponse->rowCount () == 0) {
			return 0;
		}
		$donnee = $reponse->fetch ();
		return $donnee['num_cde'];
	}

	/**
	 *
	 * Insert un commandeFactureDO dans la base de donnee
	 *
	 * @param CommandeFactureDO $commandeFactureDO
	 */
	public function insertCommandeFactureDO(CommandeFactureDO $commandeFactureDO){
		$this->PrepareCommandeFactureDOToDonnee($commandeFactureDO);

		$connection = ConnectionFactory::getConnection ();
		$req = $connection->prepare("INSERT INTO cde_fact(num_cde, date_cde, date_fact, mt_fact, payee, terminee, colis_fait, num_resp , num_lieu) VALUES (:num_cde, :date_cde, :date_fact, :mt_fact, :payee, :terminee, :colis_fait, :num_resp, :num_lieu)");

		$req->execute(array(
			'num_cde' => $commandeFactureDO->getNumeroCommande(),
			'date_cde' => $commandeFactureDO->getDateCommande(),
			'date_fact' => $commandeFactureDO->getDateFacture(),
			'mt_fact' => $commandeFactureDO->getMontantFacture(),
			'payee' => $commandeFactureDO->estPaye(),
			'terminee' => $commandeFactureDO->estTerminee(),
			'colis_fait' => $commandeFactureDO->estColisFait(),
			'num_resp' => $commandeFactureDO->getNumeroResponsable(),
			'num_lieu' => $commandeFactureDO->getNumeroLieu()
		));
	}

	/**
	 * Modifie dans la base un commandeFactureDO
	 *
	 * @param CommandeFactureDO $commandeFactureDO
	 */
	public function updateCommandeFactureDO(CommandeFactureDO $commandeFactureDO){
		$this->PrepareCommandeFactureDOToDonnee($commandeFactureDO);

		$connection = ConnectionFactory::getConnection ();
		$req = $connection->prepare("UPDATE cde_fact SET date_cde = :date_cde, date_fact = :date_fact, mt_fact = :mt_fact, payee = :payee, terminee = :terminee, colis_fait = :colis_fait, num_resp = :num_resp, num_lieu = :num_lieu WHERE num_cde = :num_cde");

		$req->execute(array(
			'num_cde' => $commandeFactureDO->getNumeroCommande(),
			'date_cde' => $commandeFactureDO->getDateCommande(),
			'date_fact' => $commandeFactureDO->getDateFacture(),
			'mt_fact' => $commandeFactureDO->getMontantFacture(),
			'payee' => $commandeFactureDO->estPaye(),
			'terminee' => $commandeFactureDO->estTerminee(),
			'colis_fait' => $commandeFactureDO->estColisFait(),
			'num_resp' => $commandeFactureDO->getNumeroResponsable(),
			'num_lieu' => $commandeFactureDO->getNumeroLieu()
		));

		print_r($req->errorInfo());
	}

	/**
	 * Supprime de la base une commandeFactureDO
	 * @param CommandeFactureDO $commandeFactureDO
	 */
	public function deleteCommandeFactureDO(CommandeFactureDO $commandeFactureDO){
		$connection = ConnectionFactory::getConnection ();
		$req = $connection->prepare("DELETE FROM cde_fact WHERE num_cde = :num_cde");;
		$req->execute(array(
			'num_cde' => $commandeFactureDO->getNumeroCommande(),
		));
	}


	private function PrepareCommandeFactureDOToDonnee(CommandeFactureDO $commandeFactureDO){
		if($commandeFactureDO->estPaye() == null){
			$commandeFactureDO->setEstPaye("false");
		}
		if($commandeFactureDO->estTerminee() == null){
			$commandeFactureDO->setEstTerminee("false");
		}
		if( $commandeFactureDO->estColisFait() == null){
			$commandeFactureDO->setEstColisFait("false");
		}
	}

	private function setCommandeFactureDO($donnee){
		$commandeFactureDO = new CommandeFactureDO();
		$commandeFactureDO->setEstPaye($donnee['payee']);
		$commandeFactureDO->setNumeroLieu($donnee['num_lieu']);
		$commandeFactureDO->setEstTerminee($donnee['terminee']);
		$commandeFactureDO->setNumeroCommande($donnee['num_cde']);
		$commandeFactureDO->setEstColisFait($donnee['colis_fait']);
		$commandeFactureDO->setNumeroResponsable($donnee['num_resp']);
		$commandeFactureDO->setMontantFacture($donnee['mt_fact']);

		$dateCommande = new DateTime($donnee['date_cde']);
		$commandeFactureDO->setDateCommande($dateCommande->format('d/m/Y'));

		$dateFacture = new DateTime($donnee['date_fact']);
		$commandeFactureDO->setDateFacture($dateFacture->format('d/m/Y'));

		return $commandeFactureDO;
	}

}